sergeant
A CLI solution with simple argument parsing and built in help messages.
Example
const command = require('sergeant')
const assert = require('assert')
command('hello', ({option, parameter, command}) => {
parameter('name', {
description: 'the name',
required: true
})
option('loud', {
description: 'say it loud',
alias: 'l'
})
command('world', ({option}) => {
option('loud', {
description: 'say it loud',,
alias: 'l'
})
return (args) => {
say('world', args.loud)
}
})
return (args) => {
assert.notEqual(args.name, 'world', 'use hello world')
say(args.name, args.loud)
}
})(process.argv.slice(2))
function say (name, loud = false) {
let message = `hello ${name}!`
if (loud) {
message = message.toUpperCase() + '!'
}
console.log(message)
}
API Reference
command
command(name, description, define)
- name: A string. The name of your command
- description: A string. A short description. Optional
- define: A function to define your command
define
define({[option, parameter, command})
It should return a function to call when the command is run. See action
option
option(key, definition)
- key: The name of the option
- definition: An object that defines your option
parameter
parameter(key, definition)
- key: The name of the parameter
- definition: An object that defines your parameter
action
action(args)
It's passed args which are all the options and parameters when run.
definition
{alias, description, multiple, required, type}
- alias: Optional. A short alias for the option.
- description: A short description
- multiple: Optional. A boolean. If true then multiple values are accepted.
- required: Optional. A boolean. If true it is required.
- type: A function that should return the default if no value is given.